﻿using CommonTools.Class;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CommonTools
{
    public static class HttpHelper
    {
        public static string HttpGet(string Url, string postDataStr, string contentType = "application/json",string encoding="UTF-8")
        {
            return HttpGet(Url, postDataStr, null, contentType,encoding);
        }
        public static string HttpGet(string Url, string postDataStr,Proxy proxy, string contentType = "application/json",string encoding="UTF-8")
        {
            var request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = contentType;
            //request.SendChunked = true;
            //request.TransferEncoding = encoding;
            if(proxy!=null)
                request.Proxy=new WebProxy(proxy.IP, proxy.Port);
            var response = (HttpWebResponse)request.GetResponse();
            var myResponseStream = response.GetResponseStream();
            var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encoding));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public static string HttpPost(string url, string postDataStr, Proxy proxy, string sign = "", int timeout = 5000)
        {
            CookieContainer cookie = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


            if (proxy != null)
                request.Proxy = new WebProxy(proxy.IP, proxy.Port);
            request.Timeout = Convert.ToInt32(timeout);
            request.Method = "POST";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "application/json;charset=utf-8";
            //request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            request.CookieContainer = cookie;

            //增加密钥验证
            if (!string.IsNullOrWhiteSpace(sign))
                request.Headers.Add("sign", sign);

            byte[] postdatabtyes = Encoding.UTF8.GetBytes(postDataStr);
            request.ContentLength = postdatabtyes.Length;
            Stream requeststream = request.GetRequestStream();
            requeststream.Write(postdatabtyes, 0, postdatabtyes.Length);

            requeststream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();

            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        public static HtmlNodeCollection GetNodeFormHtml(string html,string expression)
        {
            HtmlDocument doccc = new HtmlDocument();
            doccc.LoadHtml(html);
            return doccc.DocumentNode.SelectNodes(expression);
        }
        public static string[] GetNodeValue(string html,string expression)
        {
            var nodes=GetNodeFormHtml(html, expression);
            List<string> result = new List<string>();
            foreach(var node in nodes)
            {
                result.Add(node.InnerText);
            }
            return result.ToArray();
        }
    }
}
